package com.probox.common.entity.service.order.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.BuyShowEnum;
import com.probox.common.core.enums.CartIsShowEnum;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.SaleStatusEnum;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.StringUtils;
import com.probox.order.api.domain.OrderQuery;
import com.probox.common.entity.domain.order.OrderInfoItem;
import com.probox.common.entity.mapper.order.OrderInfoItemMapper;
import com.probox.common.entity.service.order.IOrderInfoItemService;
import com.probox.order.api.domain.ItemSettlementVo;
import com.probox.order.api.domain.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 订单明细Service业务层处理
 *
 * @author hds
 * @date 2021-05-27
 */
@Service
@Transactional
public class OrderInfoItemServiceImpl extends ServiceImpl<OrderInfoItemMapper, OrderInfoItem> implements IOrderInfoItemService {
    @Autowired
    private OrderInfoItemMapper orderInfoItemMapper;


    /**
     * 查询订单明细
     *
     * @param itemUid 订单明细ID
     * @return 订单明细
     */
    @Override
    public OrderInfoItem selectOrderInfoItemById(Long itemUid) {
        return this.getById(itemUid);
    }

    /**
     * 查询订单明细列表
     *
     * @param orderInfoItem 订单明细
     * @return 订单明细
     */
    @Override
    public List<OrderInfoItem> queryList(OrderInfoItem orderInfoItem) {
        QueryWrapper<OrderInfoItem> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getItemUid())) && orderInfoItem.getItemUid() != null) {
            queryWrapper.eq("ITEM_UID", orderInfoItem.getItemUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSupplierUid())) && orderInfoItem.getSupplierUid() != null) {
            queryWrapper.eq("SUPPLIER_UID", orderInfoItem.getSupplierUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getOrderUid())) && orderInfoItem.getOrderUid() != null) {
            queryWrapper.eq("ORDER_UID", orderInfoItem.getOrderUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getActivityProUid())) && orderInfoItem.getActivityProUid() != null) {
            queryWrapper.eq("ACTIVITY_PRO_UID", orderInfoItem.getActivityProUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getProUid())) && orderInfoItem.getProUid() != null) {
            queryWrapper.eq("PRO_UID", orderInfoItem.getProUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getProName())) && orderInfoItem.getProName() != null) {
            queryWrapper.eq("PRO_NAME", orderInfoItem.getProName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSkuUid())) && orderInfoItem.getSkuUid() != null) {
            queryWrapper.eq("SKU_UID", orderInfoItem.getSkuUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSkuDesc())) && orderInfoItem.getSkuDesc() != null) {
            queryWrapper.eq("SKU_DESC", orderInfoItem.getSkuDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getImgUrl())) && orderInfoItem.getImgUrl() != null) {
            queryWrapper.eq("IMG_URL", orderInfoItem.getImgUrl());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getPrice())) && orderInfoItem.getPrice() != null) {
            queryWrapper.eq("PRICE", orderInfoItem.getPrice());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getCostPrice())) && orderInfoItem.getCostPrice() != null) {
            queryWrapper.eq("COST_PRICE", orderInfoItem.getCostPrice());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getShowPrice())) && orderInfoItem.getShowPrice() != null) {
            queryWrapper.eq("SHOW_PRICE", orderInfoItem.getShowPrice());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getItemNumber())) && orderInfoItem.getItemNumber() != null) {
            queryWrapper.eq("ITEM_NUMBER", orderInfoItem.getItemNumber());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getItemAmount())) && orderInfoItem.getItemAmount() != null) {
            queryWrapper.eq("ITEM_AMOUNT", orderInfoItem.getItemAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getPayAmount())) && orderInfoItem.getPayAmount() != null) {
            queryWrapper.eq("PAY_AMOUNT", orderInfoItem.getPayAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getUseCouponAmount())) && orderInfoItem.getUseCouponAmount() != null) {
            queryWrapper.eq("USE_COUPON_AMOUNT", orderInfoItem.getUseCouponAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getDiscountAmount())) && orderInfoItem.getDiscountAmount() != null) {
            queryWrapper.eq("DISCOUNT_AMOUNT", orderInfoItem.getDiscountAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getPayByBalance())) && orderInfoItem.getPayByBalance() != null) {
            queryWrapper.eq("PAY_BY_BALANCE", orderInfoItem.getPayByBalance());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getCartUid())) && orderInfoItem.getCartUid() != null) {
            queryWrapper.eq("CART_UID", orderInfoItem.getCartUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSettlementStatus())) && orderInfoItem.getSettlementStatus() != null) {
            queryWrapper.eq("SETTLEMENT_STATUS", orderInfoItem.getSettlementStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSettlementTime())) && orderInfoItem.getSettlementTime() != null) {
            queryWrapper.eq("SETTLEMENT_TIME", orderInfoItem.getSettlementTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getSaleStatus())) && orderInfoItem.getSaleStatus() != null) {
            queryWrapper.eq("SALE_STATUS", orderInfoItem.getSaleStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getDataStatus())) && orderInfoItem.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", orderInfoItem.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getCreateUid())) && orderInfoItem.getCreateUid() != null) {
            queryWrapper.eq("CREATE_UID", orderInfoItem.getCreateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoItem.getUpdateUid())) && orderInfoItem.getUpdateUid() != null) {
            queryWrapper.eq("UPDATE_UID", orderInfoItem.getUpdateUid());
        }
        return orderInfoItemMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单明细
     *
     * @param orderInfoItem 订单明细
     * @return 结果
     */
    @Override
    public boolean insertOrderInfoItem(OrderInfoItem orderInfoItem) {

        return this.save(orderInfoItem);

    }

    /**
     * 修改订单明细
     *
     * @param orderInfoItem 订单明细
     * @return 结果
     */
    @Override
    public boolean updateOrderInfoItem(OrderInfoItem orderInfoItem) {

        return this.updateById(orderInfoItem);
    }

    /**
     * 批量删除订单明细
     *
     * @param itemUids 需要删除的订单明细ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderInfoItemByIds(Long[] itemUids) {
        return this.removeByIds(Arrays.asList(itemUids));
    }

    @Override
    public List<OrderInfoItem> select(List<Long> orderIds) {
        QueryWrapper<OrderInfoItem> queryWrapper = Wrappers.query();
        queryWrapper.in("ORDER_UID",orderIds);
        queryWrapper.eq("DATA_STATUS","1");
        return this.orderInfoItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<OrderInfoItem> selectByOrderId(Long orderId) {
        QueryWrapper<OrderInfoItem> queryWrapper = Wrappers.query();
        queryWrapper.eq("ORDER_UID",orderId);
        queryWrapper.eq("DATA_STATUS","1");
        return this.orderInfoItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<OrderInfoItem> selectLuwenOrderList(OrderQuery query) {
        return orderInfoItemMapper.selectLuwenOrderList(query);
    }

    @Override
    public Integer selectLuwenOrderListCount(OrderQuery query) {
        return orderInfoItemMapper.selectLuwenOrderListCount(query);
    }

    /**
     * 根据订单ID查询订单明细
     * @param orderUid
     * @return
     */
    @Override
    public List<OrderItemVo> getByOrderUid(Long orderUid) {

        return orderInfoItemMapper.getByOrderUid(orderUid);
    }

    /**
     * 修改结算状态
     * @param settlementApi
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean updateSettlement(ItemSettlementVo settlementApi) {
        if(settlementApi.getItemIds()!=null && ArrayUtil.isNotEmpty(settlementApi.getItemIds())){
            String settlementStatus = settlementApi.getSettlementStatus();
            if(StringUtils.isEmpty(settlementStatus)){
                throw new BaseException("结算状态不能为空");
            }

            orderInfoItemMapper.updateSettlement(settlementStatus,settlementApi.getItemIds());

        }else {
            throw new BaseException("ID不能为空");
        }
        return true;
    }

    /**
     * 修改订单明细售后状态
     * @param itemUid
     */
    @Override
    public void updateSaleStateToServer(Long itemUid) {
        orderInfoItemMapper.updateSaleStateToServer(itemUid);
    }

    /**
     * 取消售后
     * @param itemId
     */
    @Override
    public void cancelSaleState(Long itemId) {
        orderInfoItemMapper.cancelSaleState(itemId);
    }

    /**
     * 根据商品ID查询订单ID
     * @param query
     * @return
     */
    @Override
    public List<Long> getByProId(OrderQuery query) {
        return orderInfoItemMapper.getByProId(query);
    }

    /**
     * 完成
     * @param itemUid
     */
    @Override
    public void finishSale(Long itemUid) {
        orderInfoItemMapper.finishSale(itemUid);
    }

    /**
     * 我的订单明细
     * @param customerUid
     * @return
     */
    @Override
    public List<OrderItemVo> getItemList(Long customerUid) {

        return orderInfoItemMapper.getItemList(customerUid);
    }

    @Override
    public Integer getItemListCount(Long customerUid) {
        List<OrderItemVo> itemList = orderInfoItemMapper.getItemList(customerUid);
        if(ObjectUtil.isNotEmpty(itemList))
            return itemList.size();
        return 0;
    }

    /**
     * 根据订单ID和商品名称查询
     * @param query
     * @return
     */
    @Override
    public List<OrderItemVo> getByOrderUidAndProName(OrderQuery query) {

        return orderInfoItemMapper.getByOrderUidAndProName(query);
    }

    /**
     * 余额支付信息
     * @param orderUid
     * @return
     */
    @Override
    public List<OrderItemVo> getOrderItem(Long orderUid) {
        if(ObjectUtil.isEmpty(orderUid))
            throw new BaseException("缺少订单ID参数");
        return orderInfoItemMapper.getOrderItem(orderUid);
    }

    @Override
    public Integer getOrderItemCount(Long orderUid) {
        List<OrderItemVo> list = orderInfoItemMapper.getOrderItem(orderUid);
        if(ObjectUtil.isNotEmpty(list))
            return list.size();
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean updateBuyShow(Long itemUid) {
        if(itemUid == null)
            throw new BaseException("缺少订单明细ID参数");
        OrderInfoItem item = this.getById(itemUid);
        if(ObjectUtil.isEmpty(item))
            throw new BaseException("没有找到订单明细信息");

        item.setBuyShow(BuyShowEnum.show.getKey());

        return this.updateById(item);
    }

    @Override
    public Long countAmount(OrderQuery query) {
        return orderInfoItemMapper.countAmount(query);
    }


}
